package com.whlilong.jxc.dialog;

import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.whlilong.jxc.model.Depart;
import com.whlilong.jxc.sys.ExtentionInfo;
import com.swtdesigner.ResourceManager;

public class DepartInfoCreate extends AbstractDialog {

	private List<Depart> list = ExtentionInfo.getInstance().getDepartInfoList();
	private TableViewer tableViewer;
	private Text textDepartid;
	private Text textDepartname;

	public DepartInfoCreate(Shell parentShell, TableViewer tableViewer) {
		super(parentShell);
		this.tableViewer = tableViewer;
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("新建数据");
	}

	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, Add_id, "继续添加(&A)", false);
		createButton(parent, IDialogConstants.OK_ID, "添加(&S)", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "取消(&X)", false);

	}

	public Control createGroupText(Composite parent) {
		FormLayout formLayout = (FormLayout) parent.getLayout();
		formLayout.marginBottom = 20;
		formLayout.marginTop = 20;

		CTabFolder tabFolder = new CTabFolder(parent, SWT.BORDER);
		FormData fd_tabFolder = new FormData();
		fd_tabFolder.right = new FormAttachment(100);
		fd_tabFolder.left = new FormAttachment(5);
		tabFolder.setLayoutData(fd_tabFolder);
		tabFolder.setSimple(false);
		tabFolder.setSelectionBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));

		CTabItem tabItem = new CTabItem(tabFolder, SWT.NONE);
		tabItem.setImage(ResourceManager.getPluginImage("com.whlilong.jxc",
				"icons/brand1.gif"));
		tabItem.setText("基本信息");

		Composite composite = new Composite(tabFolder, SWT.BORDER);
		tabItem.setControl(composite);
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.marginRight = 100;
		gl_composite.marginLeft = 100;
		gl_composite.marginTop = 10;
		gl_composite.verticalSpacing = 20;
		gl_composite.marginBottom = 20;
		composite.setLayout(gl_composite);

		Label labelDepartid = new Label(composite, SWT.NONE);
		labelDepartid.setBounds(0, 0, 60, 17);
		labelDepartid.setText("部门编号：");

		textDepartid = new Text(composite, SWT.BORDER);
		GridData gd_textDepartid = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_textDepartid.widthHint = 120;
		textDepartid.setLayoutData(gd_textDepartid);
		textDepartid.setBounds(0, 0, 73, 23);
		textDepartid.setFocus();

		Label labelDepartname = new Label(composite, SWT.NONE);
		labelDepartname.setBounds(0, 0, 61, 17);
		labelDepartname.setText("部门名称：");

		textDepartname = new Text(composite, SWT.BORDER);
		GridData gd_textDepartname = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_textDepartname.widthHint = 120;
		textDepartname.setLayoutData(gd_textDepartname);
		textDepartname.setBounds(0, 0, 73, 23);

		CTabItem tabItem_1 = new CTabItem(tabFolder, SWT.NONE);
		tabItem_1.setText("辅助信息");

		return parent;

	}

	protected void continueAdd() {
		if (!textDepartname.getText().equals("")
				&& textDepartid.getText() != null
				&& !textDepartname.getText().equals("")
				&& textDepartname.getText() != null) {
			for (int i = 0; i < list.size(); i++) {
				if (textDepartid.getText().equals(list.get(i).getDepartid())) {
					MessageDialog.openError(getShell(), "错误", "编号重复，添加失败！");
					return;
				}
			}
			exeSearch();
			initial();
			tableViewer.setInput(list);
			tableViewer.getTable().select(list.size() - 1);

		} else {
			MessageDialog.openError(getShell(), "错误", "编号或名称为空，添加失败！");
		}

	}

	private void initial() {
		textDepartid.setText("");
		textDepartname.setText("");

	}

	protected void exeSearch() {
		if (!textDepartname.getText().equals("")
				&& textDepartid.getText() != null
				&& !textDepartname.getText().equals("")
				&& textDepartname.getText() != null) {
			for (int i = 0; i < list.size(); i++) {
				if (textDepartid.getText().equals(list.get(i).getDepartid())) {
					MessageDialog.openError(getShell(), "错误", "编号重复，添加失败！");
					return;
				}
			}
			Depart depart = new Depart();
			if (list.size() > 0)
				depart.setId(list.get(list.size() - 1).getId() + 1);
			else
				depart.setId(1);
			depart.setDepartid(textDepartid.getText());
			depart.setDepartname(textDepartname.getText());

			list.add(depart);
			isAdd = true;
		} else {
			MessageDialog.openError(getShell(), "错误", "编号或名称为空，添加失败！");
			isAdd = false;
		}
	}

	protected void okPressed() {
		if (isAdd) {
			setReturnCode(OK);
			close();
		}
	}

	public boolean isAdd() {
		return isAdd;
	}

	public void setAdd(boolean isAdd) {
		this.isAdd = isAdd;
	}

	public List<Depart> getList() {
		// TODO Auto-generated method stub
		return list;
	}

	public void setList(List<Depart> list) {
		this.list = list;
	}
}
